package top.heliming.youyou.item.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.heliming.youyou.common.enums.ExceptionEnum;
import top.heliming.youyou.common.exception.LyException;
import top.heliming.youyou.item.dao.SpecGroupMapper;
import top.heliming.youyou.item.dao.SpecParamMapper;
import top.heliming.youyou.item.dao.SpecificationMapper;
import top.heliming.youyou.item.pojo.SpecGroup;
import top.heliming.youyou.item.pojo.SpecParam;
import top.heliming.youyou.item.pojo.Specification;
import top.heliming.youyou.item.service.SpecificationService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/30 下午 9:29
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {
  @Autowired
  private SpecificationMapper specificationMapper;

  @Autowired
  private SpecParamMapper specParamMapper;
  @Autowired
  private SpecGroupMapper specGroupMapper;
  @Override
  public Specification queryById(Long category_id) {
    return this.specificationMapper.selectByPrimaryKey(category_id);
  }
  @Override
  public List<SpecParam> querySpecParams(Long gid, Long cid, Boolean searching, Boolean generic) {
    SpecParam param = new SpecParam();
    param.setGroupId(gid);
    param.setCid(cid);
    param.setSearching(searching);
    param.setGeneric(generic);
    return this.specParamMapper.select(param);
  }

  @Override
  public List<SpecGroup> querySpecGroupByCid(Long cid) {
    SpecGroup specGroup = new SpecGroup();
    specGroup.setCid(cid);
    List<SpecGroup> specGroupList = specGroupMapper.select(specGroup);
    if (CollectionUtils.isEmpty(specGroupList)) {
      throw new LyException(ExceptionEnum.SPEC_GROUP_NOT_FOUND);
    }
    return specGroupList;
  }

  @Override
  public List<SpecGroup> querySpecsByCid(Long cid) { // 查询规格组
    List<SpecGroup> groups = this.querySpecGroupByCid(cid);
    SpecParam param = new SpecParam();
    groups.forEach(g -> {
      // 查询组内参数
      g.setParams(this.querySpecParams(g.getId(), null, null, null));
    });
    return groups;
  }
}
